package com.ebupt.migu.music.effectStatistics.controller;

import com.ebupt.migu.music.common.auth.IDAuthUtils;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.util.EasyExcelUtil;
import com.ebupt.migu.music.common.util.ReqestParamUtil;
import com.ebupt.migu.music.effectStatistics.entity.vo.MessageExportVo;
import com.ebupt.migu.music.effectStatistics.service.MessageNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 消息通知控制层
 * @author xuhongsong@cd.ebupt.com
 * @version 1.0
 * @className MessageNoticeController
 * @date 2020/7/6 16:04
 */
@RestController
@RequestMapping("messageNotice")
public class MessageNoticeController {


    //  消息通知业务层
    @Autowired
    private MessageNoticeService  messageNoticeService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private IDAuthUtils idAuthUtils;
    @Autowired
    private HttpServletResponse response;

    /**
     * 分页、条件查询
     * 查询条件： 创建时间、策略名称、执行类型、目标人群
     *
     * @return
     */
    @GetMapping("/queryMap")
    public ResultObject queryMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        return messageNoticeService.queryMap(params);
    }


    /**
     * 查询条件： 创建时间、策略名称、执行类型、目标人群
     * 全量导出
     *
     * @return
     */
    @RequestMapping(value = "/exportMap", produces = MediaType.TEXT_HTML_VALUE)
    public void exportMap() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        if(params.containsKey("createStartTime") && params.get("createStartTime").equals("undefined")){
            params.remove("createStartTime");
        }
        if(params.containsKey("createEndTime") && params.get("createEndTime").equals("undefined")){
            params.remove("createEndTime");
        }
        if(params.containsKey("executeStartTime") && params.get("executeStartTime").equals("undefined")){
            params.remove("executeStartTime");
        }
        if(params.containsKey("executeEndTime") && params.get("executeEndTime").equals("undefined")){
            params.remove("executeEndTime");
        }
        List<MessageExportVo> list = messageNoticeService.exportMap(params);
        EasyExcelUtil.export(response,MessageExportVo.class,list,"消息通知");
    }



    /**
     * 对应的详情列表
     *
     *
     * @return
     */
    @GetMapping("/turnOverList")
    public List<HashMap<String, Object>> turnOverList() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        return messageNoticeService.turnOverList(params);
    }

    /**
     *  效果统计消息通知 -- 目标人群条件查询
     *
     * @return
     */
    @PostMapping("/targetUser")
    public ResultObject targetUser() {
        Map<String, Object> params = ReqestParamUtil.getParamsWithUserInfo(request);
        params = idAuthUtils.isAdmin(params);
        return messageNoticeService.targetUser(params);
    }

}
